﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class sysadmin_SystemInfo_TeacherLoginNum : PowerCreator.LMS.Web.Base.BasePage
{
    protected PowerCreator.LMS.ViewQuery.ViewQuery obj = new PowerCreator.LMS.ViewQuery.ViewQuery();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            ViewState["OrderList"] = "";  //
            ViewState["OrderType"] = " desc ";//排序方式
             if (Request.QueryString["TrainclaID"] != null)
                ViewState["TrainclaID"] = Request.QueryString["TrainclaID"].ToString();
           string search = GetSearch();
            //Response.Write(search);
            //Response.End();
            BinderPager(search);
            BinderRPT(search, 1);
        }
    }

    protected override void InitPage()
    {
        lbl_TeacherLoginNum_UserName.Text = GetLanageValueByKey("lbl_TeacherLoginNum_UserName", PowerCreator.LMS.Language.LanagetItemType.Page);
       // lbl_TeacherLoginNum_InfoTitle.Text = GetLanageValueByKey("lbl_TeacherLoginNum_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);

        base.InitPage();
    }


    private string GetSearch()
    {
        StringBuilder sbSearch = new StringBuilder();
        sbSearch.Append("SELECT TO_AdvancedUser.Name as Name, TO_AdvancedUser.AUser_Id as id, TO_AdvancedUser.AUserName as username,TO_AdvancedUser.LoginDate as UserLastLoginTime, TO_AdvancedUser.LoginNum as UserTotalLoginNum FROM TO_AdvancedUser  where");

        if (!string.IsNullOrEmpty(txt_UserName.Text))
        {
            sbSearch.Append(string.Format(" TO_AdvancedUser.Name like '%{0}%' and ", txt_UserName.Text));
        }
        //if (Request.QueryString["TrainclaID"] != null)
        //{
        //    sbSearch.Append(string.Format(" TUser.UserId in (select FKTrainclastuUserID from TTrainClassStudent where FKTrainclastuTrainclaID= {0}) and ", Request.QueryString["TrainclaID"].ToString()));
        //}
        sbSearch.Append(" 1=1 ");
        return sbSearch.ToString();
    }

    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = obj.Count(search);
        AspNetPager1.PageSize = LMSPageSize;
        PreAspNetPager(AspNetPager1);
    }

    private void BinderRPT(string search, int pageIndex)
    {
        DataTable dt = new DataTable();

        //string orderlist = ViewState["OrderList"].ToString();
        //if (!string.IsNullOrEmpty(orderlist))
        //    search += " order by " + orderlist + ViewState["OrderType"].ToString();
        //else
        //    search += " order by TUser.UserId desc";

        //string orderList = " OnlineuserLoginTime desc ";
        dt = obj.Query(search, LMSPageSize, pageIndex);

        //dt.Columns.Add("state");
        //for (int i = 0; i < dt.Rows.Count; i++)
        //{
        //    if (!string.IsNullOrEmpty(dt.Rows[i]["ischeck"].ToString()))
        //    {
        //        if (dt.Rows[i]["ischeck"].ToString() == "0")
        //        {
        //            dt.Rows[i]["state"] = GetLanageValueByKey("AddCourseInfo_db_d2", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        }
        //        else if (dt.Rows[i]["ischeck"].ToString() == "1")
        //        {
        //            dt.Rows[i]["state"] = GetLanageValueByKey("State_Msg2", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        }
        //        else if (dt.Rows[i]["ischeck"].ToString() == "2")
        //        {
        //            dt.Rows[i]["state"] = GetLanageValueByKey("State_Msg1", PowerCreator.LMS.Language.LanagetItemType.Page);
        //        }
        //    }
        //}
        
        rpt_TeacherLoginNum.DataSource = dt;
        rpt_TeacherLoginNum.DataBind();
    }
    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderRPT(search, AspNetPager1.CurrentPageIndex);
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderRPT(search, 1);
    }
    protected void btn_CurrentSearch_Click(object sender, EventArgs e)
    {
        
    }

    protected void rpt_TeacherLoginNum_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "orderby")
        {
            ViewState["OrderList"] = "TUser."+e.CommandArgument.ToString();
            string search = GetSearch();
            BinderRPT(search, AspNetPager1.CurrentPageIndex);
            if (ViewState["OrderType"].ToString() == " desc")
                ViewState["OrderType"] = " asc";
            else
                ViewState["OrderType"] = " desc";
        }
    }
}
